<?php
namespace QuestionPlus\Service\Import\QuestionImport;

use QuestionPlus\Service\Import\QuestionImport\AbstractQuestionImport;

use QuestionPlus\Service\Import\Import;

class MaterialQuestionImport extends AbstractQuestionImport
{
    private $choice = "[A-Za-z]\.\s";

    private $subs = array(
        'question' => "#\d+\-\d+\.#"
    );

    protected $subTypes = array(
        'single_choice' => '【单项选择题】',
        'choice' => '【多项选择题】',
        'uncertain_choice' => '【不定项选择题】',
        'fill' => '【填空题】',
        'determine' => '【判断题】',
        'essay' => '【问答题】'
    );

    private $pattern = array(
        'stem' => "【题干】",
        'answer' => "【答案】",
        'difficulty' => "【难度】",
        'score' => "【分数】",
        'analysis' => "【解析】"
    );

    protected function questionFilter ($text)
    {
        $text = $this->subTypeFilter($text);
        $typesStr = implode('|', $this->subTypes);
        $pattern = "#([\s\S]*?)(" . $typesStr . ")#";
        preg_match($pattern, $text, $matches);
        $stem = $matches[1];
        $questionImport = new Import($this->target, $this->importType);
        $questions = $questionImport->getSubQuestions($text);

        $question['stem'] = $stem;
        $question['questions'] = $questions;

        $question = $this->checkQuestion($question);

        return $this->materialFilter($question);
    }


    public function saveAnswers($text, $isSub = null)
    {
        $text = trim($text);
        $text = $this->subTypeFilter($text);
        $answerImport = new Import($this->target, $this->importType);
        $this->answers[] = $answerImport->getSubAnswers($text);
    }

    protected function checkQuestion ($question)
    {
        if (empty($this->answers)) {
            return $question;
        }
        $answer = array_shift($this->answers);

        foreach ($question['questions'] as $key => $value) {
            $question['questions'][$key] = $this->setAnswer($value, $answer[$key]);
        }

        return $question;
    }

    private function setAnswer($question, $answer)
    {
        foreach ($question as $key => $value) {
            if (empty($value) && array_key_exists($key, $answer)) {
                $question[$key] = $answer[$key];
            }
        }
        return $question;
    }

    private function subTypeFilter ($text)
    {
        $text = str_replace('【-', '【', $text);
        return $text;
    }

    private function materialFilter ($material)
    {
        $material['answer'] = array();

        if ($this->importType == 'mix') {
            return $this->filter($material);
        }

        foreach ($material['questions'] as $key => $question) {
            if (in_array($question['type'], array('choice', 'single_choice', 'uncertain_choice'))) {
                $material['questions'][$key] = $this->choiceFilter($question);
            }
            if ($question['type'] == 'fill') {
                $material['questions'][$key] = $this->fillFilter($question);
            }
        }
        return $this->filter($material);
    }

    private function choiceFilter ($question)
    {
        $isHas = preg_match_all("#[A-Za-z]#u", $question['answer'], $matches, PREG_SET_ORDER);
        $question['answer'] = array_map(function($match){
            if (ord($match[0]) < 97) {
                return (string)(ord($match[0]) - 65);
            } else {
                return (string)(ord($match[0]) - 97);
            }
        }, $matches);

        return $this->subFilter($question);
    }

    private function fillFilter ($question)
    {
        $answer = explode(' ', $question['answer']);
        $find = array_fill(0, count($answer), '___');
        $replace = array_map(function($ans){
            return '[[' . $ans . ']]';
        }, $answer);

        $question['stem'] = str_replace($find, $replace, $question['stem'], $i);

        $question['answer'] = array_map(function($ans){
            return explode('|', $ans);
        }, $answer);

        return $this->subFilter($question);
    }

    private function subFilter ($question)
    {
        if (!empty($question['difficulty'])) {
            $question['difficulty'] = array_search($question['difficulty'], $this->difficulty);
        }

        $question['target'] = $this->target;
        $question['parentId'] = '0';

        return $question;
    }

    protected function testQuestionByType($question)
    {
        
    }
}